/**
 * 平台启动完毕后，需要初始化的内容(PC)
 */
(function() {
    // 取得LOGO、站点名称
    var logoF = function() {
        var setLogo = function(v) {
            var url = context.config.url;
            if (urlInfo.params.try == 1) url = context.config.tryUrl;
            $("#domain_logo_url").attr("src", url + v);
        };
        var setKF = function(v) {
            if (v) { 
                try { eval(v); } catch (e) { console.error(v, e); }
            }
        };

        if (window.localStorage) {
            var key = urlInfo.host + "/logo";
            var v = localStorage.getItem(key);
            if (v && false) setLogo(v);
            else {
                Classify.queryExt("settings", "logo-big", false, "", function(v) {
                    if (v) setLogo(v);
                    localStorage.setItem(urlInfo.host + "/logo", v);
                });
            }

            var key = urlInfo.host + "/kf";
            var v = localStorage.getItem(key);
            if (v && false) setKF(v);
            else {
                Classify.queryExt("settings", "kf", false, "", function(v) {
                    setKF(v);
                    localStorage.setItem(urlInfo.host + "/kf", v);
                });
            }

            key = urlInfo.host + "/title";
            var v = localStorage.getItem(key);
            if (v) $("title").html(v);
            else {
                Classify.query("settings", "title", false, "", function(v) {
                    $("title").html(v);
                    localStorage.setItem(urlInfo.host + "/title", v);
                });
            }
        }
        else {
            Classify.queryExt("settings", "logo-big", false, "", function(v) {
                if (v) setLogo(v);
            });
            Classify.queryExt("settings", "kf", false, "", function(v) {
                setKF(v);
            });
            Classify.query("settings", "title", false, "", function(v) {
                $("title").html(v);
            });
        }
    }

    // 公告
    var noticeF = function() {
        Classify.queryExt("settings", "board", true, "[]", function(v) {
            var m = $("#marquee"); m.empty();
            for (var i in v) {
                var d = v[i];
                m.append("<li><a href='#'>" + d.content + "</li>");
            }

            // 实现公告的滚动效果
            $(document).ready(function() {
                if ($("#marquee").marquee)
                    $("#marquee").marquee({
                        pauseSpeed: 4000,
                        scrollSpeed: 30,
                        showSpeed: 250,
                    });
            });

            $("#marquee a").click(function() {
                // 显示列表
                var ul = $(".notice-wrap .lay-list ul"); ul.empty();
                var l = '<li><h3>__subject__</h3><p>__t__</p> <p>__c__</p> </li>';
                for (var i in v) {
                    var d = v[i];
                    var l2 = l.replace(/__subject__/, d.subject || '');
                    l2 = l2.replace(/__t__/, d.time || '');
                    l2 = l2.replace(/__c__/, d.content || '');
                    ul.append(l2);
                }

                $(".notice-wrap .lay-important").hide();
                $(".notice-wrap .lay-list").show();
                $(".notice-wrap").show();
            });
        });
    }

    // 顶部的菜单处理
    var topEventF = function() {
        // 音效开关
        var drawSound = function() {
            var div = $("#countdown_sound");
            if (Util.isMute()) {
                div.attr("class", "close_sound");
                div.attr("title", "点击开启提醒声音");
            }
            else {
                div.attr("class", "open_sound");
                div.attr("title", "点击关闭提醒声音");
            }
        };
        $("#countdown_sound").click(function() {
            if (Util.isMute()) Util.setMute(0);
            else Util.setMute(1);
            drawSound();
        });
        drawSound();

        // 刷新用户信息
        $("#refresh_userinfo").click(function() { ME.query(); });

        $("a.weijie").click(function() {
            window.notBetUI = true;
            switch_page("center/weijie", $(".content-wrap"));
        });
        $("a.jtyj").click(function() {
            window.notBetUI = true;
            switch_page("center/today", $(".content-wrap"));
        });
        $("a.kjjg").click(function() {
            window.notBetUI = true;
            switch_page("center/result", $(".content-wrap"));
        });
        $("a.yxgz").click(function() {
            window.notBetUI = true;
            switch_page("center/rule", $(".content-wrap"));
        });
        $("a.czjl").click(function() {
            window.notBetUI = true;
            context.center.menu = 'charge_log';
            switch_page("center/money", $(".content-wrap"));
        });
        $(".side_left .charge").click(function() {
            window.notBetUI = true;
            context.center.menu = 'charge';
            switch_page("center/money", $(".content-wrap"));
        });
        $(".side_left .withdraw").click(function() {
            window.notBetUI = true;
            context.center.menu = 'withdraw';
            switch_page("center/money", $(".content-wrap"));
        });
        $("a.lsbb").click(function() {
            window.notBetUI = true;
            switch_page("center/baobiao", $(".content-wrap"));
        });
        $("a.grzx").click(function() {
            window.notBetUI = true;
            switch_page("center/zixun", $(".content-wrap"));
        });
        $(".side_left .center").click(function() {
            window.notBetUI = true;
            context.center.menu = 'notice';
            switch_page("center/notice", $(".content-wrap"));
        });
        $(".side_left .activity").click(function() {
            window.notBetUI = true;
            switch_page("center/activity", $(".content-wrap"));
        });
        $(".menu3 .logout").click(function() {
            layer.confirm("确定退出系统吗？", {
                icon: 3,
                title: "用户提示",
                zIndex: 6
            }, function() {
                ME.logout(function() {
                    window.location.href = "index.html";
                });
            });
        });
        $(".menu4 .support").click(function() {
            _MEIQIA('showPanel');
        });
        $(".kfonline").click(function() {
            _MEIQIA('showPanel');
        });
    
        // 切换皮肤
        $("#skinPanel").mouseleave(function() {
            $("#skinPanel ul").hide();
        });
        $("#skinPanel").mouseenter(function() {
            $("#skinPanel ul").show();
        });
        $("#skinPanel li a").click(function(e) {
            Util.setSkin($(e.currentTarget).attr("value"));
        });
        
        $(".account-info .account_title a").click(function() {
            if (!context.user.user.mobile) {
                window.open(Util.genUrl({
                    file: 'index.html',
                    register: 1,
                }));
                return;
            }
            window.open(Util.genUrl({
                file: 'center.html'
            }));
        });
    }

    // 缓存
    var cache = function() {
        var preLoadGame = function(g, i) {
            setTimeout(function() {
                preload_page(g);
            }, 200 * i);
        }
        for (var i = 0; i < 9; i++) {
            if (i >= GameList.menus.length) break;
            preLoadGame(GameList.menus[i].game, i);
        }
    };

    window.startup = function(cb) {
        var ti = new Date().getTime();

        // 账户信息改变的回调
        ME.cb = function() {
            set_account_info(ME.get_name(), ME.balance(), ME.pending());
            if (context.user) {
                if (!context.user.user.mobile) {
                    // 游客，一些按钮隐藏掉
                    $(".on-off-charge").hide();
                    $(".on-off-center").hide();
                    $(".on-off-support").hide();
                    $(".on-off-agent").hide();
                    $(".on-off-register").show();
                    $(".on-off-support").show();
                    $(".lsbb").parent().hide();
                    $(".czjl").parent().hide();
                    $(".kjjg").parent().find("e").hide();
                }
                else {
                    $(".on-off-charge").show();
                    $(".on-off-center").show();
                    $(".on-off-support").show();
                    $(".on-off-register").hide();
                    $(".lsbb").parent().show();
                    $(".czjl").parent().show();
                    $(".kjjg").parent().find("e").show();
                    if (context.user.user.grade == 49) {
                        $(".on-off-agent").show();
                        $(".on-off-agent a").attr("href", "../referee/index.html");
                    }
                }
            }
            Result.todayProfit(function(r) {
                $("#total_sum_money").html("(" + (r.amount/100).toFixed(2) + ")");
            }, function() {
                $("#total_sum_money").html("(0)");
            });
        }
        ME.query();

        // 获取游戏列表
        GameList.init(function() {
            Result.pullLastOpen();
            Result.pullNext();
            
            cache();
            if (cb) cb();

            var t2 = new Date().getTime();
            if (t2 - ti > 2000)
                $("#appLoading").hide();
            else {
                setTimeout(function() {
                    $("#appLoading").hide();
                }, 1500);
            }
        });

        layer.config({
            skin: 'demo-class'
        });
        logoF();

        // 延迟显示公告
        setTimeout(function() {
            // 公告
            noticeF();
        }, 2000);

        // 弹出公告
        Message.init();

        // 顶部菜单处理
        topEventF();

        // 定期抓取赢的数据
        Util.registerTimer("bet_win_query", 5, function() {
            Result.bet_win_query();
        });
        Result.cb = function() {
            var c = Result.last();
            if (c)
                set_lastest_result(c.issue, c.red);
            else
                set_lastest_result();

            // 倒计时
            c = Result.curr();
            if (c) {
                var open = c.open / 1000,
                    end = c.end / 1000;
                context.curr[game] = c.issue;
                set_countdown(parseInt(open - get_server_time()), parseInt(end - get_server_time()), c.issue, c.betEnable);
            }
        };
        Result.refreshListCb = function() {
            var list = gameRule.long(context.result[game]);
            set_long_queue(list);
            initLuZhu(window.game);
        };
        Result.gotWin = function(list) {
            // 显示提示框
            show_bonus_clue(list);
        };
                
        Lottery.cb = function() {
            var lottery = context.lottery[game];
            if (!isEmpty(lottery)) {
                // 填充进来
                var map = gameRule.map;
                for (var k in lottery) {
                    if (k == "targets") continue;
                    if (isEmpty(map[k])) {
                        console.log("不存在的类型", k);
                        continue;
                    }
                    map[k].rate = lottery[k].value;
                }
            }
            refresh_bet_items();
    
            // 填充下注列表
            refresh_curr_bet_list();
        };

        // 掉线检测
        ME.assureConnect();

        // 定期刷新我的信息
        Util.registerTimer("refresh_me", 10, function() {
            ME.query();
        });
    }
})();
